
#include "batminton.h"
#include "ui_batminton.h"
#include "ball.h"
#include "player.h"
#include <cmath>
#include <iostream>
Batminton::Batminton(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::Batminton)
{
    ui->setupUi(this);
    setWindowTitle("Batminton");
    myTimer=new QTimer(this);
    myTimer->setInterval(17);
    player[0]=new Player;
    player[1]=new Player;
    ball=new Ball;
//初始化人、球的位置坐标，计分器
    ball->x()=ui->ball->geometry().x();
        ball->y()=ui->ball->geometry().y();
        score[0]=score[1]=0;
    player[0]->x()=200;
    player[0]->y()=805;
    player[1]->x()=1340;
    player[1]->y()=805;

    //每17ms更新位置和计数器
    connect(myTimer, &QTimer::timeout,[=](){
        GameCalculate();
        drawboard();
    });
}

Batminton::~Batminton()
{
    delete ui;
}
//设置Start、Resume、Pause、reset按钮
void Batminton::on_buttonReset_clicked(){
    if (myTimer->isActive()==true){
        myTimer->stop();
    }
    score[0]=score[1]=0;
    ui->buttonStart->setText("Start");
    player[0]->reset();
    player[1]->reset();
    ball->updateball();
    drawboard();
}
void Batminton::on_buttonStart_clicked(){
    if (myTimer->isActive()==false){
        myTimer->start();
        ui->buttonStart->setText("Pause");
    }else{
        myTimer->stop();
        ui->buttonStart->setText("Continue");
    }
}

//将按键与相应函数相连
void Batminton::keyPressEvent(QKeyEvent *event)
{
    switch (event->key()){
        case Qt::Key_Up:
            player[1]->Jump();
            break;
        case Qt::Key_Left:
            player[1]->moveleft();
            break;
        case Qt::Key_Right:
            player[1]->moveright();
            break;
        case Qt::Key_W:
            player[0]->Jump();
            break;
        case Qt::Key_A:
            player[0]->moveleft();
            break;
        case Qt::Key_D:
            player[0]->moveright();
            break;
        case Qt::Key_S:
            player[0]->pshoot();
            break;

        case Qt::Key_Down:
            player[1]->pshoot();
            break;

    }
}
void Batminton::keyReleaseEvent(QKeyEvent *event){
    switch (event->key()){
    case Qt::Key_Left:
        player[1]->stopleft();
        break;
    case Qt::Key_Right:
        player[1]->stopright();
        break;
    case Qt::Key_A:
        player[0]->stopleft();
        break;
    case Qt::Key_D:
        player[0]->stopright();
        break;
    case Qt::Key_S:
        player[0]->pStopShoot();
        break;

    case Qt::Key_Down:
        player[1]->pStopShoot();
        break;

    }
}
void Batminton::drawboard(){
    ui->Player_1->setGeometry(player[0]->x(),player[0]->y(),60,60);
    ui->Player_2->setGeometry(player[1]->x(),player[1]->y(),60,60);
    ui->Score_1->display(score[0]);
    ui->Score_2->display(score[1]);
    ui->ball->setGeometry(ball->x(),ball->y(),16,16);
}
bool check(double distance,int bx,int by,int px){
    return (distance<=150 && (by<=492 or (bx-800)*(px+30-800)>0));
}
//击球
void Batminton::Shoot(){
    for (int i=0;i<2;i++){
        double d=sqrt(pow((ball->x()+8)-(player[i]->x()+30),2)+pow(ball->y()+8-(player[i]->y()+30),2));
        //计算人和球的几何中心的距离
        if (player[i]->checkshoot()){
            if (check(d,ball->x(),ball->y(),player[i]->x())){

                ball->ballshoot(player[i]->x(),player[i]->y(),d);
                player[i]->pStopShoot();
            }
        }

    }
}
//球的移动
void Batminton::Moveball(){
    ball->moveball();
    if (ball->checkifonland()){
        score[(ball->area())]++;
        K^=1;
        ui->toShoot->setGeometry(1600-(ui->toShoot->geometry().x()),ui->toShoot->geometry().y(),16,16);
        ball->Stop();
    }
    ball->reflectball();
    if (!ball->checkMove()){
        if (K==0) ball->setposition(player[K]->x()+40,player[K]->y()+2);
        else ball->setposition(player[K]->x()+2,player[K]->y()+2);
    }
}
//击球者移动
void Batminton::Moveplayer(){
    for (int i=0;i<2;i++){
        player[i]->trulyleft();
        player[i]->trulyright();
        player[i]->update();
    }
}
//位置检查
void Batminton::Positioncheck(){
    if (player[1]->x()<800) player[1]->x()=800;
    if (player[0]->x()>800-60) player[0]->x()=800;
    if (player[1]->x()+60>1600){
        player[1]->x()=1540;
        player[1]->stopright();
    }
    if (player[0]->x()<0){
        player[0]->x()=0;
        player[0]->stopleft();
    }

    if (player[1]->x()<800){
        player[1]->stopMoveX();
        player[1]->x()=800;
        player[1]->stopleft();
    }
    if (player[0]->x()+60>840){

        player[0]->stopMoveX();
        player[0]->x()=800-60;
        player[0]->stopright();
    }
    for (int i=0;i<2;i++){
        if (player[i]->y()>805){
            player[i]->y()=805;
            player[i]->setJump();
        }
    }
}
void Batminton::Ballupdate(){
   ball->updateball();
}
//位置与数据更新
void Batminton::GameCalculate(){
    Moveball();
    Shoot();
    Moveplayer();
    Positioncheck();
    Ballupdate();
}

